package com.zhoug.zmui.group;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 支持多行的RadioGroup
 *
 * @Author 35574
 * @Date 2021/12/23
 * @Description
 */
public class ZMUIRadioGroup extends RadioGroup {
    private static final String TAG = ">>>ZMUIRadioGroup";
    private static  boolean debug=false;

    public static final int LAYOUT_GRID = 1;//网格布局
    private int mLayout = LAYOUT_GRID;//默认网格布局
    private int gridWidthSpan = 3;//网格布局一行显示几个按钮
    private int rowSpacing = 20;//行间距

    public ZMUIRadioGroup(Context context) {
        super(context);
    }

    public ZMUIRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ZMUIRadioGroup, 0, 0);
        mLayout = array.getInt(R.styleable.ZMUIRadioGroup_zmui_Layout, LAYOUT_GRID);
        rowSpacing = array.getDimensionPixelOffset(R.styleable.ZMUIRadioGroup_zmui_rowSpacing, 20);
        gridWidthSpan = array.getInt(R.styleable.ZMUIRadioGroup_zmui_gridWidthSpan, 3);
        if(gridWidthSpan <=0){
            gridWidthSpan =3;
        }
        array.recycle();

    }

    //每一行的高度
    private HashMap<Integer, Integer> mLineHeight = new HashMap<>();


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        mLineHeight.clear();
        if (mLayout == LAYOUT_GRID && childCount > 0) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            //宽度已知
            if (widthMode == MeasureSpec.EXACTLY) {
                //child的最大宽度
                int childMaxWidth = (widthSize - getPaddingStart() - getPaddingEnd()) / gridWidthSpan;
                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childMaxWidth, MeasureSpec.AT_MOST);
                int lineHeight = 0;
                for (int i = 0; i < childCount; i++) {
                    View child = getChildAt(i);
                    measureChild(child, childWidthMeasureSpec, heightMeasureSpec);
                    int childHeight = child.getMeasuredHeight();
                    //行号
                    int lineNum = i / gridWidthSpan;
                    if (i % gridWidthSpan == 0) {
                        //0-0,1-1,2-2,3-0
                        if (debug){
                            LogUtils.d(TAG, "onMeasure:第" + lineNum + "行");
                        }
                        lineHeight = 0;
                    }
                    lineHeight = Math.max(lineHeight, childHeight);
                    mLineHeight.put(lineNum, lineHeight);
                }
                int lines = childCount / gridWidthSpan;
                if (childCount % gridWidthSpan != 0) {
                    lines++;
                }
                if (debug){
                    LogUtils.d(TAG, "onMeasure:总" + lines + "行");
                }
                int height = getPaddingTop() + getPaddingBottom() + rowSpacing * (lines - 1);
                for (Map.Entry<Integer, Integer> entry : mLineHeight.entrySet()) {
                    height += entry.getValue();
                }
                if (debug){
                    LogUtils.d(TAG, "onMeasure:height = " + height);
                }
                setMeasuredDimension(widthSize, height);
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }


    @SuppressWarnings("all")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (debug){
            LogUtils.d(TAG, "onLayout:"+String.format(Locale.CANADA,"%d,%d,%d,%d",l,t,r,b));
        }
        int childCount = getChildCount();
        if (mLayout == LAYOUT_GRID && childCount > 0) {
            int left = 0;
            int top = getPaddingTop();
            int right = 0;
            int bottom = 0;
            int measuredWidth = getMeasuredWidth();
            int childMaxWidth = (measuredWidth - getPaddingStart() - getPaddingEnd()) / gridWidthSpan;

            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                left = getPaddingStart() + childMaxWidth * (i % gridWidthSpan);
                if (i % gridWidthSpan == 0) {
                    int lineNum = i / gridWidthSpan;
                    //0-0,1-1,2-2,3-0
                    if (debug){
                        LogUtils.d(TAG, "onMeasure:第" + lineNum + "行");
                    }
                    if (lineNum > 0) {
                        top += mLineHeight.get(lineNum - 1) + rowSpacing;
                    }

                }

                right = left + child.getMeasuredWidth();
                bottom = top + child.getMeasuredHeight();
                child.layout(left, top, right, bottom);
            }

        } else {
            super.onLayout(changed, l, t, r, b);
        }

    }

}
